#include <fstream>
#include <string>
#include <string.h>
#include <functional>

#include "common/HiCommonLog.hpp"

using namespace std;
string Byte2Hex(const uint8_t* bArray, size_t bArray_len);
size_t hexCharToByte(uint8_t* data, size_t len, const char* line);

bool readRTP(const char* file, const function<void(const uint8_t*, size_t)>& OnRead) {
	ifstream fin(file);
	if (!fin.good()) {
		HI_DEBUG("file:%s not exist", file);
		return false;
	}
	
	HI_DEBUG("file open success");
	
	static const size_t LEN = 5120;
	
	static const size_t HALF_LEN = LEN/2;
	char buf[LEN] = {0};
	uint8_t dist[HALF_LEN] = {0};	
	while (!fin.eof()) {
		::memset(buf, 0, LEN);
		fin.getline(buf, LEN);
		
		if (nullptr == buf) {
			continue;
		}
		
		if (buf[0] != '[') {
			
			continue;
		}
		unsigned int len = strlen(buf);
		if (buf[len - 1] != ']') {
			continue;
		}
		buf[len - 1] = '\0';
		
		char* line = (char*)&buf[1];
		::memset(dist, 0, HALF_LEN);
		size_t size = hexCharToByte(dist, HALF_LEN, line);	
		OnRead(dist, size);
	}
	return true;
}